package com.lcifn.middleware.rpc.transport.command;

import lombok.AllArgsConstructor;
import lombok.Data;
import org.omg.CORBA.portable.ResponseHandler;

import java.nio.charset.StandardCharsets;

/**
 * 响应Header
 * @author lichao
 * @date 2021/1/31 17:33
 */

@Data
@AllArgsConstructor
public class ResponseHeader extends Header{
    private int code;
    private String error;

    public ResponseHeader(Header header) {
        this(header, Code.SUCCESS.getCode(), Code.SUCCESS.getMessage());
    }

    public ResponseHeader(Header header, int code, String error) {
        super(header.getRequestId(), header.getType(), header.getVersion());
        this.code = code;
        this.error = error;
    }

    @Override
    public int length() {
        return Integer.BYTES + Integer.BYTES + Integer.BYTES + Integer.BYTES  + Integer.BYTES
                + (error == null ? 0 : error.getBytes(StandardCharsets.UTF_8).length);
    }
}
